package hsintiao.com.xintiaoAPP.service.impl;

import hsintiao.com.xintiaoAPP.entity.Doctor;
import hsintiao.com.xintiaoAPP.entity.Goods;
import hsintiao.com.xintiaoAPP.entity.vo.DoctorVO;
import hsintiao.com.xintiaoAPP.mapper.DoctorMapper;
import hsintiao.com.xintiaoAPP.mapper.GoodsMapper;
import hsintiao.com.xintiaoAPP.service.DoctorService;
import hsintiao.com.xintiaoAPP.util.AjaxResult;
import hsintiao.com.xintiaoAPP.util.ResponseCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DoctorServiceImpl implements DoctorService {
    @Autowired
    private DoctorMapper doctorMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Override
    public AjaxResult findAll() {
        try {
            DoctorVO doctorVO = new DoctorVO();
            List<Doctor> all = doctorMapper.findAll();
            List<Goods> goods = goodsMapper.getAll();
            //非空判断
            if(all == null || all.size()==0){
                return AjaxResult.me().setCode(ResponseCode.FAILURE.getCode())
                        .setSuccess(false).setMessage("查询失败");
            }
            List<String> carousel = doctorMapper.getCarousel();
            if(carousel == null || carousel.size()==0){
                return AjaxResult.me().setCode(ResponseCode.FAILURE.getCode())
                        .setSuccess(false).setMessage("查询失败");
            }
            if(goods == null || goods.size() == 0){
                return AjaxResult.me().setCode(ResponseCode.FAILURE.getCode())
                        .setSuccess(false).setMessage("查询失败");
            }
            doctorVO.setCarousel(carousel);
            Goods setMeal =null;
            Goods chart =null;
            for (Goods good:goods) {
                if(good.getId()==2){
                    setMeal=good;
                }
                if(good.getId()==1){
                    chart=good;
                }
            }
            doctorVO.setSetMeal(setMeal);
            doctorVO.setChart(chart);
            doctorVO.setDoctors(all);
            return AjaxResult.me().setResultObj(doctorVO);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setCode(ResponseCode.FAILURE.getCode())
                    .setSuccess(false).setMessage("查询失败");
        }
    }

    @Override
    public AjaxResult findOneById(Long id) {
        try {
            Doctor one = doctorMapper.findOneById(id);
            if(one == null){
                return AjaxResult.me().setCode(ResponseCode.FAILURE.getCode())
                        .setSuccess(false).setMessage("查询失败");
            }
            return AjaxResult.me().setResultObj(one);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setCode(ResponseCode.FAILURE.getCode())
                    .setSuccess(false).setMessage("查询失败");
        }
    }
}
